package com.pengcheng.taskapplication.mvp.view.activity;

import android.content.Intent;
import android.view.View;

import com.example.library.BandCardEditText;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.BindCardModel;
import com.pengcheng.taskapplication.model.CheckModel;
import com.pengcheng.taskapplication.model.SmsModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenter;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.CheckBankCard;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;

import java.io.Serializable;
import java.lang.reflect.Type;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ZFT on 2020/11/6.
 */

public class BindCardActivity extends BaseActivity implements IView{

    private UserPresenter userPresenter;
    private long certId;
    private long from;
    @BindView(R.id.bank_card)
    BandCardEditText bandCardEditText;
    @Override
    protected int layoutResID() {
        return R.layout.activity_bind_idcard_layout;
    }

    @Override
    protected void initData() {
        from =getIntent().getLongExtra("from",0);
        userPresenter=new UserPresenter(this);
        checkOcr();
    }

    public void checkOcr(){
        userPresenter.checkCert(ConstantValue.RequestKey.checkCert);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.bindCard)){
            if(code.equals("2")){
                //非工行卡
                Intent intent =new Intent(context,NoICBCCardActivity.class);
                intent.putExtra("bankcardCode",bandCardEditText.getBankCardText().toString().trim());
                intent.putExtra("from",from);
                startActivity(intent);
            }else{
//                //工行卡
//
//                Gson gson= new Gson();
//                Type type =new TypeToken<SmsModel>(){}.getType();
//                SmsModel smsModel= gson.fromJson(gson.toJson(data),type);
//                Intent intent =new Intent(context,ICBCCardActivity.class);
//                intent.putExtra("bankcardCode",bandCardEditText.getBankCardText().toString().trim());
//                intent.putExtra("smsNo",smsModel.getSmsNo());
//                intent.putExtra("from",from);
//                intent.putExtra("certId",certId);
//                startActivity(intent);
                Intent intent =new Intent(context,NoICBCCardActivity.class);
                intent.putExtra("bankcardCode",bandCardEditText.getBankCardText().toString().trim());
                intent.putExtra("from",from);
                startActivity(intent);
            }
        }

        if(actionType.equals(ConstantValue.RequestKey.checkCert)) {
            CheckModel model = (CheckModel) data;
            if (model != null) {
                certId = model.getCertId();
            }
        }
    }

    @Override
    public void onFailed(String code, String msg) {

    }
    @OnClick({R.id.img_back,R.id.tv_apply})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_apply:
                String bankNum=bandCardEditText.getBankCardText().toString().trim();
                if(bankNum==null||bankNum.length()==0||!CheckBankCard.checkBankCard(bankNum)){
                    ToastUtils.showShortToast("请输入正确的银行卡号");
                    return;
                }
                BindCardModel bindCardModel =new BindCardModel();
                bindCardModel.setCertId(certId);
                bindCardModel.setBankcardCode(bankNum);
                bindCardModel.setPhone(SPUtils.getString(context,ConstantValue.SpType.phone));
                bindCard(bindCardModel);
                break;
        }
    }
    public void bindCard(BindCardModel bindCardModel){
        userPresenter.bindCard(bindCardModel, ConstantValue.RequestKey.bindCard);
    }
}
